﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// RolePermissionsHelper 的摘要说明
/// </summary>
public class RolePermissionsHelper
{
    public RolePermissionsHelper()
    {

    }

    public static DataTable getRolePermissions(string roleID)
    {
        if (CacheHelper.getCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID) == null)
        {
            DBHelper db = new DBHelper();
            DataTable dt = db.getList("SELECT ID,roleID,moduleID FROM RolePermissions WHERE roleID=" + roleID);
            CacheHelper.setCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID, dt, new TimeSpan(30, 0, 0, 0));

            return dt;
        }
        else
        {
            return CacheHelper.getCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID) as DataTable;
        }
    }

    public static DataTable getModule()
    {
        if (CacheHelper.getCache(AppConfig.CACHE_MODULE) == null)
        {
            DBHelper db = new DBHelper();
            DataTable dt = db.getList("SELECT ID,moduleName,url,icon,parentID,[index] FROM [Module] WHERE isUsing=1 ORDER BY [index] ASC");
            CacheHelper.setCache(AppConfig.CACHE_MODULE, dt, new TimeSpan(30, 0, 0, 0));

            return dt;
        }
        else
        {
            return CacheHelper.getCache(AppConfig.CACHE_MODULE) as DataTable;
        }
    }

    public static DataTable getModuleWithRolePermissions(string roleID)
    {
        if (CacheHelper.getCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID) == null)
        {

            DataTable dtModuleWithPermissions = createModuleWithRolePermissionsDataTable();

            DataTable dtModule = getModule();
            DataTable dtRolePermissions = getRolePermissions(roleID);

            foreach (DataRow dr in dtModule.Rows)
            {
                DataRow ndr = dtModuleWithPermissions.NewRow();

                if (dtRolePermissions.Select("moduleID='" + dr["ID"].ToString().Trim() + "'").Count() > 0)
                {
                    ndr["ID"] = dr["ID"];
                    ndr["moduleName"] = dr["moduleName"];
                    ndr["url"] = dr["url"];
                    ndr["icon"] = dr["icon"];
                    ndr["parentID"] = dr["parentID"];
                    ndr["index"] = dr["index"];
                    ndr["permission"] = "1";
                }
                else
                {
                    ndr["ID"] = dr["ID"];
                    ndr["moduleName"] = dr["moduleName"];
                    ndr["url"] = dr["url"];
                    ndr["icon"] = dr["icon"];
                    ndr["parentID"] = dr["parentID"];
                    ndr["index"] = dr["index"];
                    ndr["permission"] = "0";
                }

                dtModuleWithPermissions.Rows.Add(ndr);
            }

            CacheHelper.setCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID, dtModuleWithPermissions, new TimeSpan(30, 0, 0, 0));
            return dtModuleWithPermissions;
        }
        else
        {
            return CacheHelper.getCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID) as DataTable;
        }
    }

    public static string createNavigationHtml(string roleID)
    {
        if (CacheHelper.getCache(AppConfig.CACHE_NAVIGATION + roleID) == null)
        {
            string html = "";

            DataTable dtModulePermission = getModuleWithRolePermissions(roleID);

            DataRow[] drTop = dtModulePermission.Select("parentID='0' AND permission='1'");

            foreach (DataRow dr in drTop)
            {
                html += " <li class=\"treeview\">";
                html += "                <a href=\"" + dr[2] + "\">";
                html += "                    <i class=\"fa " + dr[3] + "\"></i><span>" + dr[1] + "</span>";
                html += "                </a>";
                html += "                <ul class=\"treeview-menu\">";

                DataRow[] drChild = dtModulePermission.Select("parentID='" + dr[0] + "' AND permission='1'");
                foreach (DataRow drC in drChild)
                {
                    html += "                    <li style=\"margin-left:8px;\"><a href=\"" + drC[2] + "\" class=\"mynav\"><i class=\"fa " + drC[3] + "\"></i>" + drC[1] + "</a></li>";
                }
                html += "               </ul>";
                html += "           </li>";
            }

            CacheHelper.setCache(AppConfig.CACHE_NAVIGATION + roleID, html, new TimeSpan(30, 0, 0, 0));

            return html;
        }
        else
        {
            return CacheHelper.getCache(AppConfig.CACHE_NAVIGATION + roleID) as string;
        }
    }

    public static DataTable createModuleWithRolePermissionsDataTable()
    {
        DataTable dtModuleWithPermissions = new DataTable();

        DataColumn dc1 = new DataColumn("ID");
        DataColumn dc2 = new DataColumn("moduleName");
        DataColumn dc3 = new DataColumn("url");
        DataColumn dc4 = new DataColumn("icon");
        DataColumn dc5 = new DataColumn("parentID");
        DataColumn dc6 = new DataColumn("permission");
        DataColumn dc7 = new DataColumn("index");

        dtModuleWithPermissions.Columns.Add(dc1);
        dtModuleWithPermissions.Columns.Add(dc2);
        dtModuleWithPermissions.Columns.Add(dc3);
        dtModuleWithPermissions.Columns.Add(dc4);
        dtModuleWithPermissions.Columns.Add(dc5);
        dtModuleWithPermissions.Columns.Add(dc6);
        dtModuleWithPermissions.Columns.Add(dc7);

        return dtModuleWithPermissions;
    }

    public static DataRow dataRowToModuleWithRolePermissionsDataTableRow(DataTable dt, DataRow dr)
    {
        DataRow ndr = dt.NewRow();

        ndr["ID"] = dr[0];
        ndr["moduleName"] = dr[1];
        ndr["url"] = dr[2];
        ndr["icon"] = dr[3];
        ndr["parentID"] = dr[4];
        ndr["permission"] = dr[5];
        ndr["index"] = dr[6];

        return ndr;
    }
}